package com.lightsh.gui;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;

import com.lightsh.body.BaseBody;
import com.lightsh.configure.Configure;
import com.lightsh.interfaces.listener.ITouchListener;
import com.lightsh.object.SimpleObject;
import com.lightsh.utility.Utility;

public class NormalObjectScreen extends TransferScreen{
	
	private static final String TAG = "NormalObjectScreen";
	private static final boolean DEBUG = true;
	
	protected List<SimpleObject> mObjects;
	public NormalObjectScreen(Context context, int screenW, int screenH, int totalScreenW, int totalScreenH) {
		super(context, screenW, screenH, totalScreenW, totalScreenH);
		mObjects = Collections.synchronizedList(new LinkedList<SimpleObject>());
	}
	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		// 画各个body
		final Rect rect = getCurrentRectClone();
		final float worldScale = getScreenScale();
		
		// 优化这里的绘制方法
		Utility.fixme();
		int currentLevel = -1;
		int nextLevel = 0;
		int level = 0;
		while(currentLevel != nextLevel){
			currentLevel = nextLevel;
			for(SimpleObject object : mObjects){
				level = object.getTouchLevel();
				if(level == currentLevel){
					object.draw(canvas, rect, 1, worldScale);
				}else if(level > currentLevel){
					// 记录与currentLevel最接近的下一个level
					if(currentLevel == nextLevel || nextLevel > level){
						nextLevel = level;
					}
				}
			}
		}
	}
	@Override
	public void onUpdate() {
		super.onUpdate();
		if(!isPause()){
			// 更新screen信息
			
			// 更新各个object的状态
			for(SimpleObject object : mObjects){
				object.update();
			}
		}
	}

	public void addObject(SimpleObject object){
		synchronized (mObjects) {
			mObjects.add(object);
		}
	}
	public boolean removeObject(SimpleObject object){
		synchronized (mObjects) {
			return mObjects.remove(object);
		}
	}
	@Override
	public void clearAllList() {
		super.clearAllList();
		synchronized (mObjects) {
			mObjects.clear();
		}
	}
	
	
}
